package com.runa.persistence;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuj
 */
@Slf4j
public class DateUtil {

    private static final String GENERIC_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String MILLIS_FORMAT = "yyyy-MM-dd HH:mm:ss:sss";

    private static final String TIME_FORMAT = "HH:mm:ss";

    public static final String NO_LINE_FORMAT = "yyyyMMddHHmmss";

    public static void main(String[] args) {
        String period = "11010401";
        String begin = period.substring(0, 4);
        String end = period.substring(4, 8);
        Date date = new Date();
        String year = DateUtil.dateToString(date, "yyyy");
        Date b = DateUtil.stringToDate(year + begin, "yyyyMMdd");
        Date e = DateUtil.stringToDate(year + end, "yyyyMMdd");

        System.out.println(b);
        System.out.println(e);

    }

    public static boolean heatingPeriod(String period) {
        String begin = period.substring(0, 4);
        String end = period.substring(4, 8);
        Date date = new Date();
        String year = DateUtil.dateToString(date, "yyyy");
        Date b = DateUtil.stringToDate(year + begin, "yyyyMMdd");
        Date e = DateUtil.stringToDate(year + end, "yyyyMMdd");
        if (b.after(e)) {
            if ((date.after(e) && date.before(b))) {
                // 非供暖期
                return false;
            } else {
                // 供暖期
                return true;
            }
        } else {
            if ((date.after(b) && date.before(e))) {
                // 供暖期
                return true;
            } else {
                // 非供暖期
                return false;
            }
        }
    }

    public static String nowToString(String format) {
        return dateToString(new Date(), format);
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date stringToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            log.error("", e);
        }
        return null;
    }

    public static Date toDateGeneric(String date) {
        return stringToDate(date, GENERIC_FORMAT);
    }

    public static String toStringGeneric(String date) {
        return dateToString(stringToDate(date, GENERIC_FORMAT), GENERIC_FORMAT);
    }

    public static String toStringFormat(String date, String format) {
        return dateToString(stringToDate(date, format), GENERIC_FORMAT);
    }

    public static Date noLineFormatToDate(String date) {
        return stringToDate(date, NO_LINE_FORMAT);
    }

    public static String toStringGeneric(Date date) {
        return dateToString(date, GENERIC_FORMAT);
    }

    public static String currentTime() {
        return dateToString(new Date(), "HH:mm:ss");
    }


    public static Long timestamp() {
        return new Date().getTime();
    }

    public static Long getTime(String dateTimeStr, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(dateTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return parse.getTime();
    }

    public static Long getTimeSecond(String dateTimeStr) {
        return getTime(dateTimeStr, GENERIC_FORMAT) / 1000;
    }

    public static Long subtractTime(String s1, String s2) {
        return getTimeSecond(s1) - getTimeSecond(s2);
    }

    public static boolean beyondOneHour(String time) {
        if (null == time) {
            return false;
        }
        long l = new Date().getTime() / 1000;
        Long timeSecond = getTimeSecond(time);
        long hourSecond = 1 * 60 * 60;
        return l - timeSecond > hourSecond;
    }

    public static Date nowAddMonths(Integer month) {
        return dateAddMonths(new Date(), month);
    }

    public static Date dateAddMonths(Date date, Integer month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    public static Date dateSubtractMinutes(Date date, Long minutes) {
        return new Date(date.getTime() - minutes * 60 * 1000);
    }

    public static Date dateAddMinutes(Date date, Long minutes) {
        return new Date(date.getTime() + minutes * 60 * 1000);
    }

    public static String timeNow() {
        return parseDateToStr(TIME_FORMAT, new Date());
    }

    public static String milliTimeNow() {
        return parseDateToStr(MILLIS_FORMAT, new Date());
    }

    public static String dateTimeNow() {
        return parseDateToStr(GENERIC_FORMAT, new Date());
    }

    public static String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static boolean isZero() {
        return "00:00:00".equals(currentTime());
    }


    public static byte[] getNowTimeBytes() {
        String s = dateToString(new Date(), NO_LINE_FORMAT);
        return hexStringToByte(s);
    }

    public static byte[] hexStringToByte(String str) {
        if (str.contains(" ")) {
            str = str.replaceAll(" ", "");
        }
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }


    public static String toDateString(String s) {
        int index = 0;
        String year = s.substring(index, index+4);
        index += 4;
        String month = s.substring(index, index+2);
        index += 2;
        String day = s.substring(index, index+2);
        index += 2;
        String hour = s.substring(index, index+2);
        index += 2;
        String minute = s.substring(index, index+2);
        index += 2;
        String second = s.substring(index, index+2);
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }


    public static void sleep(TimeUnit timeUnit, long timeout) {
        try {
            timeUnit.sleep(timeout);
        } catch (InterruptedException e) {
            log.error("", e);
        }
    }
}
